package com.air.common.service.imp;

import java.util.List;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.air.common.dao.SystemConfigDao;
import com.air.common.exception.DuplicateEntityException;
import com.air.common.exception.NoSuchConfigException;
import com.air.common.mo.SystemConfig;
import com.air.common.service.ISystemConfigService;
import com.air.common.util.BasicDBDictionary;
import com.air.common.util.QueryCondition;
import com.air.common.util.QueryExpression;

@Service("systemConfigService")
@Scope("singleton")
public class SystemConfigServiceImp extends BaseServiceImp implements
		ISystemConfigService, InitializingBean {
	
	@Autowired
	SystemConfigDao systemConfigMapper;
	
	private List<SystemConfig> allConfig;
	private List<SystemConfig> allAvailableConfig;
	
	
	@Override
	public void afterPropertiesSet() throws Exception {
		super.baseDao = systemConfigMapper;
		reloadConfigFromDB();
	}
	
	@Override
	public SystemConfig addConfig(String key, String value, String description,
			String type) throws Exception {
		
		SystemConfig config = new SystemConfig();
		config.setKeyName(key);
		config.setValue(value);
		config.setDescription(description);
		config.setType(type);
		return this.addConfig(config);
	}

	@Override
	public SystemConfig addConfig(SystemConfig config)
			throws Exception {
		
		QueryCondition condition =  new QueryCondition();
		condition.addQueryCondition("keyName", config.getKeyName(), QueryExpression.EQUALS);
		List<SystemConfig> configs = super.queryByCondition(condition);
		
		if (configs!=null&&configs.size()>0){
			throw new DuplicateEntityException("Can't add config,Cause already has config with KEY="+config.getKeyName());
		}
		
		config = super.insert(config);
		reloadConfigFromDB();
		return config;
	}

	@Override
	public void deleteConfigByKey(String key) throws NoSuchConfigException {
		QueryCondition condition =  new QueryCondition();
		condition.addQueryCondition("keyName", key, QueryExpression.EQUALS);
		super.deleteByCondition(condition);
		reloadConfigFromDB();
	}

	@Override
	public SystemConfig findConfigByKey(String key)
			throws NoSuchConfigException {
		
		SystemConfig targetConfig=null;
		for(SystemConfig config:this.allAvailableConfig){
			if (config.getKeyName().equalsIgnoreCase(key)){
				targetConfig = config;
				break;
			}
		}
		if (targetConfig==null){
			throw new NoSuchConfigException("No Such Config with KEY="+key+" or this config is not available!");
		}
		return targetConfig;
	}

	@Override
	public List<SystemConfig> getAllConfig() {
		return this.allConfig;
	}

	@Override
	public void updateConfigByKey(SystemConfig newConfig)
			throws NoSuchConfigException {
		QueryCondition condition =  new QueryCondition();
		condition.addQueryCondition("keyName", newConfig.getKeyName(), QueryExpression.EQUALS);
		List<SystemConfig> configs = super.queryByCondition(condition);
		
		if (configs==null||configs.size()==0){
			throw new NoSuchConfigException("No Such Config with KEY="+newConfig.getKeyName());
		}
		
		super.updateById(newConfig);
		
		reloadConfigFromDB();
	}
	
	public void reloadConfigFromDB(){
		
		QueryCondition condition = new QueryCondition();
		allConfig = super.queryByCondition(condition);
		condition.addQueryCondition("status", BasicDBDictionary._common_status_available, QueryExpression.EQUALS);
		allAvailableConfig = super.queryByCondition(condition);
		
		
	}
	
}
